// pages/applicationAfterSale/applicationAfterSale.js
import Notify from '../../components/dist/notify/notify';
import Toast from '../../components/dist/toast/toast';
var core = require("../../utils/core.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shStatus: '请选择收货状态', //收货状态
    sqReason: '请选择申请原因', //申请原因
    actions: [{
        name: '已收到货',
        checked: true
      },
      {
        name: '还没收到货',
        checked: false
      }
    ],
    actions2: [{
        name: '商品质量问题',
        checked: false
      },
      {
        name: '商品与页面描述不符',
        checked: false
      },
      {
        name: '商品漏发',
        checked: false
      },
      {
        name: '商品破损',
        checked: false
      },
      {
        name: '其他问题',
        checked: false
      }
    ],
    actionShow1Status: '', //收货状态对应索引
    actionShow2Status: '', //退款原因对应索引
    actionShow1: false, //收货状态
    actionShow2: false, //退款原因
    returnedNum: 1, //退货数量  
    type: '', //1整单退  还是  2单个商品退  
    goodid: '',
    orderid: '',
    order_goods: '', //商品信息
    refund: '', //退货信息
    maxNum: '', //最大退货数量
    textAreaValue: '', //退货说明的框的值
    ImgUrlList: [], //上传图片信息
    imgUid: '', //记录上传过程中每张img的uid
    phonenum: '', //联系电话


  },
  plusNum: function() {
    var num = this.data.returnedNum;
    if (num == this.data.maxNum) {
      Notify('最大退货数量为' + this.data.maxNum)
      return
    }
    this.setData({
      returnedNum: num + 1
    })
    this.caculatePrice();


  },
  reduceNum: function() {
    var num = this.data.returnedNum;
    if (num == 1) {
      Notify('最小退货数量为1')
      return
    }
    this.setData({
      returnedNum: num - 1
    })
    this.caculatePrice();
  },
  caculatePrice: function() { //计算价钱
    var that = this;
    var refund = this.data.refund;
    refund.price = parseFloat(refund.per_price) * parseFloat(that.data.returnedNum);
    if (that.isInteger(refund.price)) { //如果是整数
      refund.start_price = refund.price
      refund.end_price = '00'
    } else {
      arr[i].start_price = refund.price.split('.')[0]
      arr[i].end_price = refund.price.split('.')[1]
    }
    that.setData({
      refund: refund
    })
  },
  isInteger: function(num) {
    if (!isNaN(num) && num % 1 === 0) {
      return true;
    } else {
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
    if (options.type) {
      this.setData({
        type: options.type,
        orderid: options.orderid,
        goodid: options.goodid ? options.goodid : ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getInfo();
  },
  getInfo: function() {
    var that = this;
    var obj = {};
    obj.id = this.data.orderid;
    if (this.data.type == 2) {
      obj.goodsid = this.data.goodid;
    }
    core.get("groupapp/refund", obj, function(t) { //评论
 
      if (t.error == 0) {
        that.processData(t)
      } else {
        Toast(t.message)
      }
    })
  },
  processData: function(t) {
    var arr = t.order_goods;
    for (var i = 0; i < arr.length; i++) {
      arr[i].start_price = arr[i].goodsprice.split('.')[0]
      arr[i].end_price = arr[i].goodsprice.split('.')[1] ? arr[i].goodsprice.split('.')[1] : '00'
    }
    var refund = t.refund;
    refund.start_price = refund.price.split('.')[0]
    refund.end_price = refund.price.split('.')[1] ? refund.price.split('.')[1] : '00'


    this.setData({
      order_goods: arr,
      refund: refund,
      returnedNum: t.refund.total,
      maxNum: t.refund.total,
    })
  },
  textareaChange: function(e) {
    var value = e.detail.value
    this.setData({
      textAreaValue: value
    })

  },
  onactionClose1: function() {
   
    this.setData({
      actionShow1: false,
    })
  },
  showChooseStatus: function() {
    this.setData({
      actionShow1: true,
    })
  },
  onactionClose2: function() {
 
    this.setData({
      actionShow2: false,
    })
  },
  showtkStatus: function() {
    this.setData({
      actionShow2: true,
    })
  },

  onItemChange1: function(e) { //复选框勾选事件
   
    var index = e.target.dataset.index;
    var arr = this.data.actions
    arr[index].checked = !arr[index].checked;
    if (index == 0) { //保持单选
      arr[1].checked = !arr[index].checked;
    } else {
      arr[0].checked = !arr[index].checked;
    }

    this.setData({
      actions: arr
    })

  },
  onItemChange: function(e) { //复选框勾选事件
    var index = e.target.dataset.index;
    var arr = this.data.actions2;
    if (!arr[index].checked) { //保证单选
      for (var i = 0; i < arr.length; i++) {
        arr[i].checked = false
      }
    }
    arr[index].checked = !arr[index].checked
    this.setData({
      actions2: arr
    })

  },
  actionShow1sure: function() { //第一个确认按钮
    var that = this;
    var arr = this.data.actions;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].checked) {
        that.setData({
          shStatus: arr[i].name,
          actionShow1: false,
          actionShow1Status: i
        })
      }
    }
  },
  actionShow2sure: function() { //第二个确认按钮
    var that = this;
    var arr = this.data.actions2;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].checked) {
        that.setData({
          sqReason: arr[i].name,
          actionShow2: false,
          actionShow2Status: i
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  onChange(e) {
   
    var that = this;
    this.setData({ //记录每次上传img的uid
      imgUid: e.detail.file.uid
    })
    const {
      file
    } = e.detail
    if (file.status === 'uploading') {
      this.setData({
        progress: 0,
      })
      wx.showLoading({
        title: '上传中',
      })
    } else if (file.status === 'done') {
      this.setData({
        imageUrl: file.url,
      })
    } else if (file.status === 'remove') { //删除图片
      var removeuid = file.uid; //删除的uid
      var ImgUrlList = that.data.ImgUrlList;
      var newArr = [];
      for (var i = 0; i < ImgUrlList.length; i++) { //排除掉删除的那个
        if (ImgUrlList[i].uid != removeuid) {
          newArr.push(ImgUrlList[i]);
        } else {
          console.log('检测到相等');
        }
      }
      that.setData({
        ImgUrlList: newArr
      })
    }
  },
  onSuccess(e) {
  
  },
  onFail(e) {
   
    Toast(e.message)
  },
  onComplete(e) {
    var that = this;
   
    if (e.detail.statusCode == 200) { //上传成功
      var arr = this.data.ImgUrlList;
      var str = e.detail.data;
      var obj = JSON.parse(str)
      var url = obj.files[0].url;
      var imgobj = {};
      imgobj.url = url;
      imgobj.uid = that.data.imgUid;
      arr.push(imgobj);
      this.setData({
        ImgUrlList: arr
      })
    } else {
      Toast(e.detail.errMsg)
    }


    wx.hideLoading()
  },
  onProgress(e) {
 
  },
  onPreview(e) {
  
    // const {
    //   file,
    //   fileList
    // } = e.detail
    // wx.previewImage({
    //   current: file.url,
    //   urls: fileList.map((n) => n.url),
    // })
  },
  onRemove(e) {
   
    // const {
    //   file,
    //   fileList
    // } = e.detail
    // wx.showModal({
    //   content: '确定删除？',
    //   success: (res) => {
    //     if (res.confirm) {
    //       this.setData({
    //         fileList: fileList.filter((n) => n.uid !== file.uid),
    //       })
    //     }
    //   },
    // })
  },
  oninputevent: function(e) {
    var value = e.detail.value;
    this.setData({
      phonenum: value
    })
  },
  isPhone: function(num) { //验证手机号
    if (!(/^1[34578]\d{9}$/.test(num))) {
      return false;
    } else {
      return true;
    }
  },
  submitBtn: function() { //提交申请
   
    var refund = {};
    refund.price   = this.data.refund.price;
    refund.receivetype = this.data.actionShow1Status; //收货状态
    refund.applyreasontype = this.data.actionShow2Status; //申请原因
    var imglist = this.data.ImgUrlList;
    var newimgArr = [];
    for (var i = 0; i < imglist.length; i++) {
      newimgArr.push(imglist[i].url)
    }
    refund.images = newimgArr;
    refund.total = this.data.returnedNum; //退货数量
    refund.goodsid = this.data.goodid;
    
    refund.id = this.data.orderid;
    if (this.data.textAreaValue == '') {
      Toast('请填写退货说明');
      return
    }
    refund.content   = this.data.textAreaValue;
    if (!this.isPhone(this.data.phonenum)) {
      Toast('请填写正确的手机号');
      return
    }
    refund.mobile = this.data.phonenum
    wx.showLoading({
      title: '提交中',
    })
    core.get("groupapp/refund/submit", refund, function(t) { //评论
      
      if (t.error == 0) {
        Toast.success('提交成功');
      }else{
        Toast.success('提交失败');
      }


      wx.hideLoading();
    })




  }












})